$(function () {


    $(".add-address-div").on({
        mouseenter: function () {
            $(".add_img").attr({"src": "http://localhost:8083/resource_packs/images/add_color.png"})
            $(this).css({"border-color": "#b0b0b0"})
        },
        mouseleave: function () {
            $(".add_img").attr({"src": "http://localhost:8083/resource_packs/images/add_address.png"})
            $(this).css({"border-color": "#e0e0e0"})
        }
    });
    $(".address-edit").on({
        mouseenter: function () {
            let than=$(this).children().children(".address-action")
            than.css("opacity","1")
            $(this).css("border-color","#ff6700")
        },
        mouseleave: function () {
            $(".address-action").css("opacity","0")
            $(this).css("border-color","#e0e0e0")
        }
    });
    //地址编号
    let addressId
    //新增地址窗口
    $(".add-desc").on("click",function (){
        $(".change-address-title").text("新增地址")
        $(".confirm").text("添加")
        $(".hint").css("display","block")
    })
    //修改地址窗口
    $(".address-update").on("click", function () {
        $(".change-address-title").text("修改地址")
        $(".confirm").text("修改")
        addressId=$(this).attr("address-id");
        $(".hint").css("display", "block")
    })
    //删除地址
    $(".address-delete").on("click",function (){
        let addressId=$(this).attr("address-id");
        let addressInsert = new Address_insert();
        addressInsert.addressDelete(addressId).then(res => {
            location.reload();
        })
    })

    let revise = $(".revise-name");
    let inputUtil = new Input_util();
    //动画
    inputUtil.customizeAfloatAnimation($(".name"), $(".name-title"), revise[0], 0, -20)
    //动画
    inputUtil.customizeAfloatAnimation($(".phone-number"), $(".phone-title"), revise[1], 0, -20)
    //动画
    let address = $(".specific-address");

    inputUtil.customizeAfloatAnimation(address, $(".address-character"), address, 0, -20)
    inputUtil.customizeAfloatAnimation($(".address-label"), $(".label-word"), $(".label"), 0, -20)
    let hintImg = $(".hint-img");
    hintImg.on({
        mouseenter: function () {
            hintImg.attr({"src": "http://localhost:8083/resource_packs/images/pop_ups_closes_red.png"})
        }, mouseleave: function () {
            hintImg.attr({"src": "http://localhost:8083/resource_packs/images/pop_ups_closes.png"})
        }
    });
    let addAddress = $(".add-address");

    hintImg.on("click", function () {
        $(".hint").css("display", "none")
    })
    addAddress.on("click", function () {
        $(".hint").css("display", "block")
    })
    $(".cancel").on("click", function () {
        $(".hint").css("display", "none")
    })
        //添加/修改确认
    $(".confirm").on("click", function () {
        let name= $(".revise-name .name").val()
        let phone= $(".revise-name .phone-number").val()
        let address= $(".edit-address .address-word").text()
        let fullAddress= $(".specific-address").val()
        let label= $(".address-label").val()
        let informationId=$(".confirm").attr("information-id");
        let gaud=new Gaud();
        gaud.getCurrentLatitudeAndLongitude(address).then(res => {
            let addressInsert = new Address_insert();
            let lng=res.lng;
            let lat=res.lat;
            addressInsert.addressAdd(addressId,name,phone,address,fullAddress,label,informationId,lng,lat).then(res => {
                location.reload();
            })
        })

    })

    let addressUtil = new Address_util();
    let addressValue = addressUtil.addressValue();
    let editAddress = $(".edit-address")
    let selectedRegions = $(".region-selection")
    let selectAddress = $(".select-address");
    let removeImg = $(".remove-img");

    removeImg.on("click", function () {
        selectAddress.empty();
        selectedRegions.css("display", "none")
    })

    editAddress.on("click",function () {
        //地址
        let address = "";
        for (const address of addressValue) {
            //显示省份
            selectAddress.append("<a class='choose-province'>"+address.name+"</a>")
        }
        let province = $(".choose-province")
        //显示市
        province.on("click",function () {
            let thisProvince = $(this).text();

            address += thisProvince;
            $(".address-word").text(address)

            // 使用数组的 find() 方法查询数据
            let result = addressValue.find(function(item) {
                province.text("选择城市/地区")
                return item.name === thisProvince;
            });
            //显示城市
            selectAddress.empty();
            for (const resultIndex in result.city) {
                selectAddress.append("<a class='city'>"+result.city[resultIndex].name+"</a>")
            }

            //显示区
            $(".city").on("click",function () {
                province.text("选择区县")
                let thisDistrict = $(this).text();
                address += " "+thisDistrict;
                $(".address-word").text(address)
                let city = null;
                addressValue.find(function(item) {
                    for (const cityIndex in item.city) {
                        if (item.city[cityIndex].name === thisDistrict){
                            city = item.city[cityIndex]
                            break;
                        }
                    }
                });
                selectAddress.empty();
                for (const districtIndex in city.area) {
                    selectAddress.append("<a class='district'>"+city.area[districtIndex]+"</a>")
                }
                $(".district").on("click",function () {
                    address += " "+$(this).text();
                    console.log(address)
                    $(".address-word").text(address)
                    selectAddress.empty();
                    selectedRegions.css("display", "none")
                })
            })
        })
        selectedRegions.css("display", "block")
    })

//快速定位
    let gaud = new Gaud();
    $(".positioning").on("click", function () {
        gaud.getCity().then(res => {
            $(".address-word").text(res.province + " " + res.city + " " + res.district)
            $(".region-selection").css("display", "none")
            $(".select-address").empty()

        });
        gaud.getCurrentLocation().then(res => {
            console.log(res)
            $(".specific-address").val(res)
            inputUtil.customAnimationAids($(".specific-address"), $(".address-character"), $(".specific-address"), 0, -20)
        })
    })

})